package com.jackie.tongzhuang.Controls;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.FenQiXiangQingActivity;
import com.jackie.tongzhuang.MainActivity;
import com.jackie.tongzhuang.Models.FenQi;
import com.jackie.tongzhuang.R;

public class FenQiList extends LinearLayout implements View.OnClickListener{
    private TextView rqtext,jetext,bztext;
    private ImageView ychimg,xqimg;
    private CheckBox xzcheck;

    private String rq,je,bz;
    private boolean ych,xz;
    private int fqid;
    private boolean showcheck;
    private Context context;

    public FenQiList(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.control_fenqilist,this);
        rqtext = view.findViewById(R.id.fql_rq);
        jetext = view.findViewById(R.id.fql_je);
        bztext = view.findViewById(R.id.fql_bz);
        ychimg = view.findViewById(R.id.fql_ychimg);
        xqimg = view.findViewById(R.id.fql_xqimg);
        xzcheck = view.findViewById(R.id.fql_check);

        xqimg.setOnClickListener(this);

        TypedArray t = context.obtainStyledAttributes(attrs,R.styleable.FenQiList);
        rq = t.getString(R.styleable.FenQiList_riqi);
        bz = t.getString(R.styleable.FenQiList_beizhu);
        je = t.getString(R.styleable.FenQiList_jine);
        ych = t.getBoolean(R.styleable.FenQiList_ischanghuan,false);
        xz = t.getBoolean(R.styleable.FenQiList_xuanze,false);
        showcheck = t.getBoolean(R.styleable.FenQiList_showcheck,true);

        if(rq != null){
            rqtext.setText(rq);
        }

        if(bz != null){
            bztext.setText(bz);
        }

        if(je != null){
            jetext.setText("¥ "+BaseClass.getDecimalToStr(2,Double.valueOf(je)));
        }

        if(ych){
            ychimg.setVisibility(View.VISIBLE);
        }else{
            ychimg.setVisibility(View.GONE);
        }

        if(showcheck){
            xzcheck.setVisibility(View.VISIBLE);
        }else{
            xzcheck.setVisibility(View.GONE);
        }
        xzcheck.setChecked(xz);


        xzcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                xz = b;
                triggerParentEvent();
            }
        });

        t.recycle();

    }

    public void setId(int id){
        fqid = id;
    }

    public int getId(){
        return fqid;
    }

    public void setRiQi(String rq){
        this.rq = rq;
        rqtext.setText(rq);
    }

    public String getRiQi(){
        return rq;
    }

    public String getJinE() {
        return je;
    }

    public void setJinE(String je) {
        this.je = je;
        jetext.setText("¥ "+BaseClass.getDecimalToStr(2,Double.valueOf(je)));
    }

    public String getBeiZhu() {
        return bz;
    }

    public void setBeiZhu(String bz) {
        this.bz = bz;
        bztext.setText(bz);
    }

    public boolean isYiChangHuan() {
        return ych;
    }

    public void setYiChangHuan(boolean ych) {
        this.ych = ych;
        if(ych){
            ychimg.setVisibility(View.VISIBLE);
        }else{
            ychimg.setVisibility(View.GONE);
        }
    }

    public boolean isXuanZe() {
        return xz;
    }

    public void setXuanZe(boolean xz) {
        this.xz = xz;
        xzcheck.setChecked(xz);
    }

    public boolean isShowcheck(){return showcheck;}
    public void setShowcheck(boolean show){
        this.showcheck = show;
        if(show){
            xzcheck.setVisibility(View.VISIBLE);
        }else{
            xzcheck.setVisibility(View.GONE);
        }
    }

    public void setData(FenQi fq){
        this.setId(fq.getId());
        this.setJinE(BaseClass.getDecimalToStr(2,fq.getFenQiJinE()));
        this.setBeiZhu(fq.getFenQiRiQi()+" "+fq.getJinHuoDan().getGongHuoShang().getMingCheng()+" 分期 第 "+fq.getQiShu()+" 期");
        this.setRiQi(fq.getChangHuanRiQi());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fql_xqimg:
                Intent xqitent = new Intent(context, FenQiXiangQingActivity.class);
                xqitent.putExtra("id",fqid);
                context.startActivity(xqitent);
                break;
        }
    }

    public interface XiangQingCheckedListener{
        void onChecked(int id);
    }

    public XiangQingCheckedListener listener;

    public void setOnXiangQingCheckedListener(XiangQingCheckedListener _listener){
        listener = _listener;
    }

    public void triggerParentEvent() {
        if (listener != null) {
            listener.onChecked(fqid);
        }
    }
}
